﻿
namespace UserDefinedForms.Windows.Forms
{
	/// <summary>
	/// abstract ControlType typed datagridview
	/// </summary>
	[System.ComponentModel.DesignerCategoryAttribute("code")]
	public abstract class ControlTypeDataGridViewBase : System.Windows.Forms.UserControl
	{	
		/// <summary> 
		/// Thrown when the current Entity changed.
		/// </summary>
		public event System.EventHandler<ControlTypeDataGridViewEventArgs> CurrentEntityChanged;
		
		#region Fields
				
		/// <summary> 
		/// the current entity.
		/// </summary>
		private Entities.ControlType _currentControlType = null;
		//private Dictionary<string, string> _columnsHeaders = new Dictionary<string, string>();
		
		/// <summary> 
		/// the datagridview instance
		/// </summary>
		protected System.Windows.Forms.DataGridView uxControlTypeDataGridView;
		
		/// <summary> 
		/// the errorprovider
		/// </summary>
		protected System.Windows.Forms.ErrorProvider uxControlTypeErrorProvider;
		
		/// <summary> 
		/// the bindingsource
		/// </summary>
		protected System.Windows.Forms.BindingSource uxControlTypeBindingSource;
		
		
		/// <summary> 
		/// the DGV column associated with the ControlTypeId property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxControlTypeIdDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the Name property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxNameDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the Status property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxStatusDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the Description property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxDescriptionDataGridViewColumn;
		
		#endregion
					
		#region Combo list
		
		#endregion
		
		#region Main Datasource
		
		private Entities.TList<Entities.ControlType> _ControlTypeList;
				
		/// <summary> 
		/// The list of ControlType to be displayed in the DGV.
		/// </summary>
		[System.ComponentModel.Category("Data")]
		public Entities.TList<Entities.ControlType> ControlTypeList
		{
			get {return this._ControlTypeList;}
			set
			{
				this._ControlTypeList = value;
				this.uxControlTypeBindingSource.DataSource = null;
				this.uxControlTypeBindingSource.DataSource = value;
				this.uxControlTypeDataGridView.DataSource = null;
				this.uxControlTypeDataGridView.DataSource = this.uxControlTypeBindingSource;				
				//this.uxControlTypeBindingSource.ListChanged += new System.ComponentModel.ListChangedEventHandler(uxControlTypeBindingSource_ListChanged);
				this.uxControlTypeBindingSource.CurrentItemChanged += new System.EventHandler(OnControlTypeBindingSourceCurrentItemChanged);
			}
		}
		
		private void OnControlTypeBindingSourceCurrentItemChanged(object sender, System.EventArgs e)
		{
			_currentControlType = uxControlTypeBindingSource.Current as Entities.ControlType;
			
			if (_currentControlType != null)
			{
				_currentControlType.Validate();
			}
			//_ControlType.Validate();
			OnCurrentEntityChanged();
		}

		//void uxControlTypeBindingSource_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
		//{
			//throw new Exception("The method or operation is not implemented.");
		//}		
		
		#endregion
		
		/// <summary>
		/// Gets the currently selected <see cref="Entities.ControlType"/> instance.
		/// </summary>
		public Entities.ControlType SelectedControlType
		{
			get {return this._currentControlType;}
		}
		
		#region other properties
		
		/// <summary>
		/// Gets or sets a value indicating whether you have provided your own data-management operations for the DataGridView control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool VirtualMode
		{
			get {return this.uxControlTypeDataGridView.VirtualMode;}
			set
			{
				this.uxControlTypeDataGridView.VirtualMode = value;
			}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the option to add rows is displayed to the user.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToAddRows
		{
			get {return this.uxControlTypeDataGridView.AllowUserToAddRows;}
			set {this.uxControlTypeDataGridView.AllowUserToAddRows = value;}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the user is allowed to delete rows from the DataGridView.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToDeleteRows
		{
			get {return this.uxControlTypeDataGridView.AllowUserToDeleteRows;}
			set {this.uxControlTypeDataGridView.AllowUserToDeleteRows = value;}
		}
		
		/// <summary>
		/// Gets the <see cref="DataGridView"/> instance.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public System.Windows.Forms.DataGridView DataGridView
		{
			get {return this.uxControlTypeDataGridView;}
		}
		
		/// <summary>
		/// Gets a collection that contains all the columns in the control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]		
		[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Visible)]
		//[System.ComponentModel.EditorAttribute("System.Windows.Forms.Design.DataGridViewColumnCollectionEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))] 
		public System.Windows.Forms.DataGridViewColumnCollection Columns
		{
			get { return this.uxControlTypeDataGridView.Columns; }
		}
		
		#endregion
		
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
	
		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}
	
		#region Constructor
	
		/// <summary>
		/// Initializes a new instance of the <see cref="ControlTypeDataGridViewBase"/> class.
		/// </summary>
		public ControlTypeDataGridViewBase()
		{			
			this.components = new System.ComponentModel.Container();			
			
			this.uxControlTypeDataGridView = new System.Windows.Forms.DataGridView();
			this.uxControlTypeBindingSource = new System.Windows.Forms.BindingSource(this.components);
			this.uxControlTypeErrorProvider = new System.Windows.Forms.ErrorProvider(this.components);
			this.uxControlTypeIdDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxNameDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxStatusDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxDescriptionDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			((System.ComponentModel.ISupportInitialize)(this.uxControlTypeDataGridView)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxControlTypeBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxControlTypeErrorProvider)).BeginInit();
			this.SuspendLayout();
						
			// 
			// uxControlTypeErrorProvider
			// 
			this.uxControlTypeErrorProvider.ContainerControl = this;
			this.uxControlTypeErrorProvider.DataSource = this.uxControlTypeBindingSource;						
			// 
			// uxControlTypeDataGridView
			// 
			this.uxControlTypeDataGridView.AutoGenerateColumns = false;
			this.uxControlTypeDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.uxControlTypeDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
		this.uxControlTypeIdDataGridViewColumn,
		this.uxNameDataGridViewColumn,
		this.uxStatusDataGridViewColumn,
		this.uxDescriptionDataGridViewColumn			});
			this.uxControlTypeDataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
			this.uxControlTypeDataGridView.Location = new System.Drawing.Point(0, 0);
			this.uxControlTypeDataGridView.Name = "uxControlTypeDataGridView";
			this.uxControlTypeDataGridView.Size = new System.Drawing.Size(470, 300);
			this.uxControlTypeDataGridView.TabIndex = 0;	
			this.uxControlTypeDataGridView.BackgroundColor = System.Drawing.Color.WhiteSmoke;
			this.uxControlTypeDataGridView.EnableHeadersVisualStyles = false;
			this.uxControlTypeDataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.OnControlTypeDataGridViewDataError);
			this.uxControlTypeDataGridView.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnControlTypeDataGridViewCellValueNeeded);
			this.uxControlTypeDataGridView.CellValuePushed += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnControlTypeDataGridViewCellValuePushed);
			
			//
			// uxControlTypeIdDataGridViewColumn
			//
			this.uxControlTypeIdDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxControlTypeIdDataGridViewColumn.DataPropertyName = "ControlTypeId";
			this.uxControlTypeIdDataGridViewColumn.HeaderText = "ControlTypeId";
			this.uxControlTypeIdDataGridViewColumn.Name = "uxControlTypeIdDataGridViewColumn";
			this.uxControlTypeIdDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxControlTypeIdDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxControlTypeIdDataGridViewColumn.ReadOnly = true;		
			//
			// uxNameDataGridViewColumn
			//
			this.uxNameDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxNameDataGridViewColumn.DataPropertyName = "Name";
			this.uxNameDataGridViewColumn.HeaderText = "Name";
			this.uxNameDataGridViewColumn.Name = "uxNameDataGridViewColumn";
			this.uxNameDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxNameDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxNameDataGridViewColumn.ReadOnly = false;		
			//
			// uxStatusDataGridViewColumn
			//
			this.uxStatusDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxStatusDataGridViewColumn.DataPropertyName = "Status";
			this.uxStatusDataGridViewColumn.HeaderText = "Status";
			this.uxStatusDataGridViewColumn.Name = "uxStatusDataGridViewColumn";
			this.uxStatusDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxStatusDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxStatusDataGridViewColumn.ReadOnly = false;		
			//
			// uxDescriptionDataGridViewColumn
			//
			this.uxDescriptionDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxDescriptionDataGridViewColumn.DataPropertyName = "Description";
			this.uxDescriptionDataGridViewColumn.HeaderText = "Description";
			this.uxDescriptionDataGridViewColumn.Name = "uxDescriptionDataGridViewColumn";
			this.uxDescriptionDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxDescriptionDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxDescriptionDataGridViewColumn.ReadOnly = false;		
			
			// this
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.Controls.Add(this.uxControlTypeDataGridView);
			this.Name = "ControlTypeDataGridView";
			this.Size = new System.Drawing.Size(470, 300);
			((System.ComponentModel.ISupportInitialize)(this.uxControlTypeErrorProvider)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxControlTypeDataGridView)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxControlTypeBindingSource)).EndInit();
			this.ResumeLayout(false);
		}
		#endregion
				
		#region events
		
		/// <summary>
		/// Raised the CurrentEntityChanged event.
		/// </summary>
		protected void OnCurrentEntityChanged()
		{
			if (CurrentEntityChanged != null)
			{
				ControlTypeDataGridViewEventArgs args = new ControlTypeDataGridViewEventArgs();
				args.ControlType = _currentControlType;
				CurrentEntityChanged(this, args); 
			}
		}
		
		#endregion
		
		/// <summary>
		/// Strongly typed event 
		/// </summary>
		public class ControlTypeDataGridViewEventArgs : System.EventArgs
		{
			private Entities.ControlType	_ControlType;
	
			/// <summary>
			/// the  Entities.ControlType instance.
			/// </summary>
			public Entities.ControlType ControlType
			{
				get { return _ControlType; }
				set { _ControlType = value; }
			}	
		}
		
		/// <summary>
		/// Handles the DataError event of the uxControlTypeDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewDataErrorEventArgs"/> instance containing the event data.</param>
		protected void OnControlTypeDataGridViewDataError(object sender, System.Windows.Forms.DataGridViewDataErrorEventArgs e)
		{
			System.Diagnostics.Debug.WriteLine(string.Format( "{0}, {1}", e.ColumnIndex, e.RowIndex));
		}
		
		/// <summary>
		/// Handles the CellValueNeeded event of the uxControlTypeDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnControlTypeDataGridViewCellValueNeeded(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxControlTypeDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxControlTypeIdDataGridViewColumn":
						e.Value = ControlTypeList[e.RowIndex].ControlTypeId;
						break;
					case "uxNameDataGridViewColumn":
						e.Value = ControlTypeList[e.RowIndex].Name;
						break;
					case "uxStatusDataGridViewColumn":
						e.Value = ControlTypeList[e.RowIndex].Status;
						break;
					case "uxDescriptionDataGridViewColumn":
						e.Value = ControlTypeList[e.RowIndex].Description;
						break;
				default:
				break;
			}
		}
		
		/// <summary>
		/// Handles the CellValuePushed event of the uxControlTypeDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnControlTypeDataGridViewCellValuePushed(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxControlTypeDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxControlTypeIdDataGridViewColumn":
						ControlTypeList[e.RowIndex].ControlTypeId = (System.Int32)e.Value;
						break;
					case "uxNameDataGridViewColumn":
						ControlTypeList[e.RowIndex].Name = (System.String)e.Value;
						break;
					case "uxStatusDataGridViewColumn":
						ControlTypeList[e.RowIndex].Status = (System.Int32)e.Value;
						break;
					case "uxDescriptionDataGridViewColumn":
						ControlTypeList[e.RowIndex].Description = (System.String)e.Value;
						break;
				default:
				break;
			}
		}
	}
}
